package com.boarsoft.boar.batch.biz;

import java.util.List;

import org.springframework.stereotype.Component;
import org.springframework.transaction.annotation.Transactional;

import com.boarsoft.boar.batch.biz.BatchInfoBiz;
import com.boarsoft.boar.batch.entity.BatchEvent;
import com.boarsoft.boar.batch.entity.BatchExecution;
import com.boarsoft.boar.batch.entity.BatchInfo;
import com.boarsoft.common.Util;
import com.boarsoft.common.biz.SimpleBizImpl;
import com.boarsoft.common.dao.PagedResult;

@Component
public class BatchInfoBizImpl extends SimpleBizImpl implements BatchInfoBiz {
	@Override
	@Transactional(readOnly = true)
	public PagedResult<BatchInfo> list(String catalog, String key, String orderBy, int pageNo, int pageSize) {
		StringBuilder sb = new StringBuilder();
		sb.append(" from ").append(BatchInfo.class.getName());
		sb.append(" where 1=1");
		if (Util.strIsNotEmpty(catalog)) {
			sb.append(" and catalog='").append(catalog).append("'");
		}
		if (Util.strIsNotEmpty(key)) {
			sb.append(" and (code='").append(key).append("'");
			sb.append("or name='").append(key).append("')");
		}
		if (Util.strIsNotEmpty(orderBy)) {
			sb.append(" order by ").append(orderBy);
		}
		int total = dao.getTotal("select count(id)".concat(sb.toString()));
		List<BatchInfo> lt = dao.list(sb.toString());
		return new PagedResult<BatchInfo>(total, lt, pageNo, pageSize);
	}

	@Transactional(readOnly = true)
	public BatchInfo find(String code) {
		StringBuilder sb = new StringBuilder();
		sb.append("and code='").append(code).append("'");
		return dao.get(BatchInfo.class, sb.toString());
	}

	@Override
	@Transactional(readOnly = true)
	public BatchInfo get(String id) {
		return dao.get(BatchInfo.class, id);
	}

	@Override
	@Transactional
	public void save(BatchInfo o) {
		if (Util.strIsEmpty(o.getId())) {
			dao.save(o);
		} else {
			dao.merge(o);
		}
	}

	@Override
	@Transactional
	public void delete(String id) {
		// 删除执行记录的相关事件
		StringBuilder sb = new StringBuilder();
		sb.append("delete from ").append(BatchEvent.class.getName());
		sb.append(" where exeId in (select id from ");
		sb.append(BatchExecution.class.getName());
		sb.append(" where infoId='").append(id).append("')");
		dao.executeUpdate(sb.toString());
		// 删除执行记录
		sb.setLength(0);
		sb.append("delete from ").append(BatchExecution.class.getName());
		sb.append(" where infoId='").append(id).append("'");
		dao.executeUpdate(sb.toString());
		// 删除批量定义
		dao.delete(BatchInfo.class, id);
	}

	@Override
	@Transactional
	public boolean enable(String id) {
		StringBuilder sb = new StringBuilder();
		sb.append("update ").append(BatchInfo.class.getName());
		sb.append(" set status = ").append(BatchInfo.STATUS_ENABLE);
		sb.append(" where id = '").append(id);
		sb.append("' and status = ").append(BatchInfo.STATUS_DISABLE);
		return dao.executeUpdate(sb.toString()) == 1;
	}

	@Override
	@Transactional
	public boolean disable(String id) {
		StringBuilder sb = new StringBuilder();
		sb.append("update ").append(BatchInfo.class.getName());
		sb.append(" set status = ").append(BatchInfo.STATUS_DISABLE);
		sb.append(" where id = '").append(id);
		sb.append("' and status = ").append(BatchInfo.STATUS_ENABLE);
		return dao.executeUpdate(sb.toString()) == 1;
	}
}